package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.list(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public Dish save(DishDTO dishDTO) {
        // 保存菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.DISABLE); // 初始化为停售
       dishMapper.save(dish);

        // 保存口味数据
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        for (DishFlavor dishFlavor : dishFlavors) {
            dishFlavor.setDishId(dish.getId());
        }
        dishFlavorMapper.save(dishFlavors);
        return dish;
    }

    @Override
    public DishVO getById(Long id) {
        // 获取菜品的数据
        DishVO dish = dishMapper.getDishById(id);
        // 获取口味的数据
        ArrayList<DishFlavor> f = dishFlavorMapper.getByDishId(id);
        dish.setFlavors(f);
        return dish;
    }

    @Override
    @Transactional
    public Dish update(DishDTO dishDTO) {
        // 保存菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);


        // 先删除旧口味，再保存口味数据
        dishFlavorMapper.deleteByDishId(dish.getId());

        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        for (DishFlavor dishFlavor : dishFlavors) {
            dishFlavor.setDishId(dish.getId());
        }
        dishFlavorMapper.save(dishFlavors);
        return dish;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
        return;
    }

    @Override
    @Transactional
    public void deleteById(List<Long> ids) {
        // 查询菜品关联的数据，如果有责不能被删除
        Integer count = dishMapper.getDishCountByIds(ids, StatusConstant.ENABLE);

        if (count > 0) {
            // 起售中的不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE) ;
        }

        //判断当前菜品是否能够删除---是否被套餐关联了？？提示错误信息
        //去中间表查，查setmeal_id是否存在，存在即关联，存到List集合里
        Integer num=setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (num > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL) ;
        }


        // 此时再去删除所有的菜品数据
        dishMapper.deleteIds(ids);
        dishFlavorMapper.deleteByDishIds(ids);
        return;
    }

    @Override
    public List<Dish> getDishByCategoryId(Long categoryId) {
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);
        List<Dish> d = dishMapper.getDishByCategoryId(dish);
        return d;
    }
}
